//
//  ShoppingCartViewController.m
//  YiShangPin
//
//  Created by 衣尚品1 on 15/3/5.
//  Copyright (c) 2015年 yishangpin2. All rights reserved.
//

#import "ShoppingCartViewController.h"
#import "ShoppingDetailedView.h"
#import "LimitedtimeView.h"
#import "AppDelegate.h"

#define USDCNY 6.2135   //美元(USD)-人民币(CNY)
@interface ShoppingCartViewController ()
{
    AppDelegate *delegate;
    UIView *view;
    float totalPrices;
    float EstimatedToChina;
    long ShoppingXibTag;
}

@end

@implementation ShoppingCartViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    delegate = [[UIApplication sharedApplication]delegate];
    EstimatedToChina = 11.00;
    
    // Do any additional setup after loading the view.
}

-(void)viewWillAppear:(BOOL)animated{
    if (delegate.ShoppingArray.count == 0) {
        _scrollview.hidden = YES;
        _smallview.hidden = YES;
    }
    else
    {
        _scrollview.hidden = NO;
        _smallview.hidden = NO;
    [self uploadxib];
    //设置数据
    _TotalPrices.text = [NSString stringWithFormat:@"US$%.2f",totalPrices+EstimatedToChina];
    _RMB.text = [NSString stringWithFormat:@"*Approx: ￥%.2f",(totalPrices + EstimatedToChina)*USDCNY];
    }
}

-(void)viewWillDisappear:(BOOL)animated{
    [view removeFromSuperview];
    totalPrices = 0;
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

#pragma mark - 加载.Xib
- (void)uploadxib{
    view = [[UIView alloc]initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, 100)];
    int AllY = 0;
    
    for (long i = delegate.ShoppingArray.count-1; i>=0; i--) {
        
    NSArray *nib = [[NSBundle mainBundle] loadNibNamed:@"ShoppingDetailedView" owner:self options:nil];
    ShoppingDetailedView *shoppingDetailedView = nib[0];
    shoppingDetailedView.frame = CGRectMake(0, AllY, self.view.frame.size.width, shoppingDetailedView.Label.frame.size.height + 16);
        shoppingDetailedView.tag = 2000+i;
    AllY += shoppingDetailedView.frame.size.height;
    //设置图片
    shoppingDetailedView.pic.image = [UIImage imageNamed:[NSString stringWithFormat:@"fushi%d.jpg",[delegate.ShoppingArray[i] integerValue]-1000]];
    //加入总价
    totalPrices += [shoppingDetailedView.prices.text floatValue];

    [view addSubview:shoppingDetailedView];
        
    }
    
    
    
    NSArray *nib1 = [[NSBundle mainBundle] loadNibNamed:@"LimitedtimeView" owner:self options:nil];
    LimitedtimeView *limitedtimeView = nib1[0];
    limitedtimeView.frame = CGRectMake(0, AllY, self.view.frame.size.width, 172);
    AllY += limitedtimeView.frame.size.height;
    
    //设置数据
    limitedtimeView.ItemTotal.text = [NSString stringWithFormat:@"US$%.2f",totalPrices];
    limitedtimeView.Estimated.text = [NSString stringWithFormat:@"US$%.2f",EstimatedToChina];
    limitedtimeView.OrderTotal.text = [NSString stringWithFormat:@"US$%.2f",EstimatedToChina+totalPrices];
    limitedtimeView.Approx.text = [NSString stringWithFormat:@"US$%.2f",(EstimatedToChina+totalPrices)*USDCNY];

    [view addSubview:limitedtimeView];
    

    view.frame = CGRectMake(0, 0, self.view.frame.size.width, AllY);
    self.scrollview.contentSize = CGSizeMake(self.view.frame.size.width, AllY);
    [self.scrollview addSubview:view];


}

- (IBAction)StartShopping:(id)sender {
    delegate.sideViewController.rootViewController.selectedIndex = 0;
}

- (void)RemoveXib:(long)tag{
    UIAlertView *alert = [[UIAlertView alloc]initWithTitle:@"Are you sure?" message:@"Do you want to remove this from your cart" delegate:self cancelButtonTitle:@"No" otherButtonTitles:@"Yes", nil];
    [alert show];
    ShoppingXibTag = tag;
}

- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex{

    if (buttonIndex == 1) {
        [delegate.ShoppingArray removeObjectAtIndex:(ShoppingXibTag -2000)];
        [self viewWillDisappear:YES];
        [self viewWillAppear:YES];
        
    }


}
@end
